/**
 * Created by liujie on 2017/10/16.
 */
var accountDataTable;
var viewAddPage_url = "/editAccountInfo";
var AccountInfo = function ($) {

    var viewAccountList_url = "/accountManage";
    var saveAccountData_url = "/editAccountInfo/saveData";
    function initTable() {
        accountDataTable = new BootstrapTable($('#tb_departments'),{
            url: '/accountManage/queryAccountList',
            sidePagination: "server",
            toolbar: '#toolbar',
            singleSelect:false,
            height:500,
            queryParams:function (params) {
                var _params = {
                    loginName:$("#loginName").val(),
                    name:$("#name").val()
                };
                if($("#kindId").val()){
                    _params["kindId"] =$("#kindId").val();
                }
                return $.extend(params,_params);
            },
            columns: [{
                checkbox: true
            }, {
                field: 'loginName',
                title: '账号'
            }, {
                field: 'name',
                title: '账号操作人'
            }, {
                field: 'deptName',
                title: '所属用户'
            },{
                field: 'depts',
                title: '关注分组'
            },
                {
                field: 'id',
                title: '操作',
                formatter:function (value,row,index) {
                    var opHtml = "";
                    var isEdit = AuthUtils.getOpAuthById("15_38");
                    var isDel = AuthUtils.getOpAuthById("15_39");
                    if(isEdit){
                        opHtml+= "<button type=\"button\" class=\"btn  btn-theme\" style=\"margin-bottom: 3px;margin-top: 3px;margin-right: 3px;padding: 3px 6px;\" onclick='editAccountView("+row.id+")'>修 改</button>";
                    }
                    if(isDel){
                        opHtml+= "<button type=\"button\" class=\"btn btn-danger\"  style=\"margin-bottom: 3px;margin-top: 3px;padding: 3px 6px;\" onclick='deleteAccountRow("+row.id+")'>删 除</button>";
                    }
                    return opHtml

                }
            }
            //     {
            //     field: 'date',
            //     title: '起始时间'
            // }
            ]
        });
        accountDataTable.init();
    }

    /**
     * 初始化用户管理列表页面事件
     */
    function initEvent() {
        $("#btn_add").on("click",function () {
            $.link(viewAddPage_url);
        });
        $("#resetForm").on("click",function () {
            $(this).parents(".content-search").find("input").val("");
        });

        $("#dosearch").on("click",function () {
            var params = {
                loginName:$("#loginName").val(),
                name:$("#name").val(),
                //phone:$("#phone").val(),
            };
            if($("#kindId").val()){
                params["kindId"] =$("#kindId").val();
            }
            accountDataTable.refresh({query:params});
        });
        $(".selectUser").on("click",function () {
            $("#myTreeDialog").modal("show");
        });
        $(".selectUserObject").on("click",function () {
            $("#myTreeObjectDialog").modal("show");
        });
    }

    /**
     * 初始化用户管理编辑页面事件
     */
    function initEditUserInfoEvent() {
        $("#submitAccountForm").on("click",function () {
            if($("#deptId").val()==""){
                $.messageExt.alert("系统提示","所属用户为必填项","warning");
                return false
            }
            if($("#loginName").val() == ""){
                $.messageExt.alert("系统提示","账号为必填项","warning");
                return false
            }
            if($("#password").val() == ""){
                $.messageExt.alert("系统提示","密码为必填项","warning");
                return false
            }
            if($("#loginPass").val() == ""){
                $.messageExt.alert("系统提示","确认密码为必填项","warning");
                return false
            }
            if($("#password").val() != $("#loginPass").val()){
                $.messageExt.alert("系统提示","密码与确认密码不一致","warning");
                return false
            }
            if($("#memo").val() == ""){
                $.messageExt.alert("系统提示","关注分组不能为空","warning");
                return false
            }

            // if($("#name").val() == ""){
            //     $.messageExt.alert("系统提示","账号操作人是必填项","warning");
            //     return false
            // }

            saveData();
        });
        $("#back-view").click(function () {
            $.link(viewAccountList_url);
        });
        $("#userInfoForm").validForm([
            {
                name:"loginName",
                validate:{
                    type:"require",
                    message:"账号是必填项"
                }
            },
            {
                name:"loginPass",
                validate:[
                    {
                        type:"require",
                        message:"确认密码为必填项"
                    }
                ]
            },
            {
                name:"password",
                validate:[
                    {
                        type:"require",
                        message:"密码为必填项"
                    }
                ]
            }
        ])
        $(".selectUser").on("click",function () {
            $("#myTreeDialog").modal("show");
        });
        $(".selectUserObject").on("click",function () {
            $("#myTreeObjectDialog").modal("show");
        });
    }


    function saveData() {
        $.ajax({
            url:saveAccountData_url,
            type:"POST",
            data:{
                id:$("#id").val(),
                deptId:$("#deptId").val(),
                deptName:$("#deptName").val(),
                name:$("#name").val(),
                loginName:$("#loginName").val(),
                loginPass:$("#loginPass").val(),
                roleId:$("#roleId").val(),
                memo:$("#memo").val(),
                userType:$("#realUserType").val()
            },
            dataType:"json"
        }).done(function (result) {
            var _data = result.data;
            if(result.isSuccess === true){
                if($("#id").val() !=""){
                    $.messageExt.alert("系统提示",_data.msg ? _data.msg : "账号修改成功！","success");
                }else{
                    $.messageExt.alert("系统提示",_data.msg ? _data.msg : "账号添加成功！","success");
                }
                // $("#baseTerminalIdTwo").val($("#baseTerminalId").val());
                // $("#baseTerminalIdTree").val($("#baseTerminalId").val());
                $("#deptId").val("");
                $("#deptName").val("");
                $("#name").val("");
                $("#loginName").val("");
                $("#loginPass").val("");
                $("#id").val("");
                $("#password").val("");
                $("#memo").val("");
                $("#depts").val("")
            }else{
                if($("#id").val() !=""){
                    $.messageExt.alert("系统提示",_data.msg ? _data.msg : "账号修改失败！","error");
                }else{
                    $.messageExt.alert("系统提示",_data.msg ? _data.msg : "账号添加失败！","error");
                }
            }
        })
    }
    return{
        initAccountPage:function () {
            initTable();
            initEvent();
        },
        initEditUserPage:function () {
            initEditUserInfoEvent();

        }
    }
}(jQuery);
function editAccountView(id) {
    $.link(viewAddPage_url + "?id="+id);
}